package cd.cdyb.sms.controller;

import cd.cdyb.sms.dao.CompanyDao;
import cd.cdyb.sms.domain.Company;
import cd.cdyb.sms.dto.MessageCarrier;
import cd.cdyb.sms.exception.BusinessException;
import cd.cdyb.sms.service.MessageService;
import cd.cdyb.sms.utils.UserUtils;
import cn.hutool.core.lang.UUID;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

@RestController
@RequestMapping("/message")
public class MessageController {

    @Resource
    private MessageService messageService;

    @Resource
    private CompanyDao companyDao;

    /**
     * 不带参发送短信
     *
     * @param messageCarrier 短信信息
     */
    @PostMapping("/pmall/sendMessage")
    public void sendMessage(@RequestBody @Valid MessageCarrier messageCarrier) throws BusinessException {
        messageCarrier.setMsgId(UUID.randomUUID().toString().replace("-", "").substring(0, 20));
        messageService.sendMessage(messageCarrier);
    }

    /**
     * 带参发送短信
     *
     * @param messageCarrier 短信信息
     */
    @PostMapping("/pmall/sendMessageWithParam")
    public void sendMessageWithParam(@RequestBody @Valid MessageCarrier messageCarrier) throws BusinessException {
        messageCarrier.setMsgId(UUID.randomUUID().toString().replace("-", "").substring(0, 20));
        messageService.sendMessageWithParam(messageCarrier);
    }

    /**
     * 不带参发送短信
     *
     * @param messageCarrier 短信信息
     */
    @PostMapping("/sendMessage")
    public void sendMessageAuthentication(@RequestBody MessageCarrier messageCarrier) throws BusinessException {
        messageCarrier.setMsgId(UUID.randomUUID().toString().replace("-", "").substring(0, 20));
        checkParam(messageCarrier);
        messageService.sendMessage(messageCarrier);
    }

    /**
     * 带参发送短信
     *
     * @param messageCarrier 短信信息
     */
    @PostMapping("/sendMessageWithParam")
    public void sendMessageWithParamAuthentication(@RequestBody MessageCarrier messageCarrier) throws BusinessException {
        messageCarrier.setMsgId(UUID.randomUUID().toString().replace("-", "").substring(0, 20));
        checkParam(messageCarrier);
        messageService.sendMessageWithParam(messageCarrier);
    }

    private void checkParam(MessageCarrier messageCarrier) throws BusinessException {
        if (Objects.isNull(messageCarrier.getTemplateId())) {
            throw new BusinessException("模板id不能为空");
        }
        if (Objects.isNull(messageCarrier.getType())) {
            throw new BusinessException("短信类型不能为空");
        }
        if (StringUtils.isBlank(messageCarrier.getPhone())) {
            throw new BusinessException("手机号不能为空");
        }
        Integer companyId = UserUtils.getCompanyId();
        Company company = companyDao.selectById(companyId);
        if (Objects.isNull(company)) {
            throw new BusinessException("公司信息不存在");
        }
        messageCarrier.setCompanyKey(company.getCompanyKey());
        messageCarrier.setCompanySecret(company.getCompanySecret());
    }

}
